package com.aiks.single.singlemavenproject.common.exception;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.util.Collection;
import java.util.Map;
import java.util.Objects;

/**
 * 验证断言的 {@link Objects}.
 *
 * @author <a href="mailto:aiks_yuan@163.com">yuanzhen</a>
 * @date 2019-07-04 10:15
 * @since 1.0.0
 **/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class AssertHelper {
    
    /**
     * null assert.
     *
     * @param source source
     * @param errorKey key
     */
    public static void isNull(final Object source, final String errorKey) {
        isNull(source, errorKey, "");
    }
    
    /**
     * null assert.
     *
     * @param source source
     * @param errorKey key
     * @param errorValues values
     */
    public static void isNull(final Object source, final String errorKey, final String... errorValues) {
        if (Objects.nonNull(source)) {
            throw new BusinessException(errorKey, errorValues);
        }
    }
    
    /**
     * true assert.
     *
     * @param source source
     * @param errorKey key
     */
    public static void isTure(final Boolean source, final String errorKey) {
        isTure(source, errorKey, "");
    }
    
    /**
     * true assert.
     *
     * @param source source
     * @param errorKey key
     * @param errorValues values
     */
    public static void isTure(final Boolean source, final String errorKey, final String... errorValues) {
        if (!source) {
            throw new BusinessException(errorKey, errorValues);
        }
    }
    
    /**
     * not blank assert.
     *
     * @param source source
     * @param errorKey key
     */
    public static void notBlank(final String source, final String errorKey) {
        notBlank(source, errorKey, "");
    }
    
    /**
     * not blank assert.
     *
     * @param source source
     * @param errorKey key
     * @param errorValues values
     */
    public static void notBlank(final String source, final String errorKey, final String... errorValues) {
        if (source == null || source.isEmpty()) {
            throw new BusinessException(errorKey, errorValues);
        }
    }
    
    /**
     * not empty assert.
     *
     * @param source source
     * @param errorKey key
     */
    public static void notEmpty(final Collection<?> source, final String errorKey) {
        notEmpty(source, errorKey, "");
    }
    
    /**
     * not empty assert.
     *
     * @param source source
     * @param errorKey key
     * @param errorValues values
     */
    public static void notEmpty(final Collection<?> source, final String errorKey, final String... errorValues) {
        if (source == null || source.isEmpty()) {
            throw new BusinessException(errorKey, errorValues);
        }
    }
    
    /**
     * not empty assert.
     *
     * @param source source
     * @param errorKey key
     */
    public static void notEmpty(final Map<?, ?> source, final String errorKey) {
        notEmpty(source, errorKey, "");
    }
    
    /**
     * not empty assert.
     *
     * @param source source
     * @param errorKey key
     * @param errorValues values
     */
    public static void notEmpty(final Map<?, ?> source, final String errorKey, final String... errorValues) {
        if (source == null || source.isEmpty()) {
            throw new BusinessException(errorKey, errorValues);
        }
    }
    
    /**
     * not null assert.
     *
     * @param source source
     * @param errorKey key
     */
    public static void notNull(final Object source, final String errorKey) {
        notNull(source, errorKey, "");
    }
    
    /**
     * not assert.
     *
     * @param source source
     * @param errorKey key
     * @param errorValues values
     */
    public static void notNull(final Object source, final String errorKey, final String... errorValues) {
        if (Objects.isNull(source)) {
            throw new BusinessException(errorKey, errorValues);
        }
    }
    
    /**
     * not null assert.
     *
     * @param source source
     * @param errorKey key
     */
    public static void notNull(final Collection<?> source, final String errorKey) {
        notNull(source, errorKey, "");
    }
    
    /**
     * not null assert.
     *
     * @param source source
     * @param errorKey key
     * @param errorValues values
     */
    public static void notNull(final Collection<?> source, final String errorKey, final String... errorValues) {
        if (source == null) {
            throw new BusinessException(errorKey, errorValues);
        }
    }
    
    /**
     * not null assert.
     *
     * @param source source
     * @param errorKey key
     */
    public static void notNull(final Map<?, ?> source, final String errorKey) {
        notNull(source, errorKey, "");
    }
    
    /**
     * not null assert.
     *
     * @param source source
     * @param errorKey key
     * @param errorValues values
     */
    public static void notNull(final Map<?, ?> source, final String errorKey, final String... errorValues) {
        if (source == null) {
            throw new BusinessException(errorKey, errorValues);
        }
    }
    
    /**
     * equal assert.
     *
     * @param target target
     * @param source source
     * @param errorKey key
     */
    public static void isEquals(final String target, final String source, final String errorKey) {
        isEquals(target, source, errorKey, "");
    }
    
    /**
     * equal assert.
     *
     * @param target target
     * @param source source
     * @param errorKey key
     * @param errorValues values
     */
    public static void isEquals(final String target, final String source, final String errorKey, final String... errorValues) {
        isEquals(target, source, false, errorKey);
    }
    
    /**
     * equal assert.
     *
     * @param target target
     * @param source source
     * @param ignoreCase case
     * @param errorKey key
     */
    public static void isEquals(final String target, final String source, final boolean ignoreCase, final String errorKey) {
        isEquals(target, source, ignoreCase, errorKey, "");
    }
    
    /**
     * equal assert.
     *
     * @param target target
     * @param source source
     * @param ignoreCase case
     * @param errorKey key
     * @param errorValues values
     */
    public static void isEquals(final String target, final String source, final boolean ignoreCase, final String errorKey, final String... errorValues) {
        String targetSwap = target;
        String sourceSwap = source;
        if (ignoreCase) {
            targetSwap = targetSwap.toLowerCase();
            sourceSwap = sourceSwap.toLowerCase();
        }
        if (!Objects.equals(targetSwap, sourceSwap)) {
            throw new BusinessException(errorKey, errorValues);
        }
    }
    
    /**
     * equal assert.
     *
     * @param target target
     * @param source source
     * @param errorKey key
     */
    public static void isEquals(final Object target, final Object source, final String errorKey) {
        isEquals(target, source, errorKey, "");
    }
    
    /**
     * equal assert.
     *
     * @param target target
     * @param source source
     * @param errorKey key
     * @param errorValues value
     */
    public static void isEquals(final Object target, final Object source, final String errorKey, final String... errorValues) {
        if (!target.equals(source)) {
            throw new BusinessException(errorKey, errorValues);
        }
    }
    
}
